package com.xxl.job.executor.core.executor;

import com.xxl.job.core.biz.AdminBiz;
import com.xxl.job.core.biz.model.HandleCallbackParam;
import com.xxl.job.core.biz.model.RegistryParam;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.util.GsonTool;
import com.xxl.job.core.util.XxlJobRemotingUtil;
import com.xxl.job.executor.feign.ProvideClient;

import java.util.List;

/**
 * 仿照com.xxl.job.core.biz.client.AdminBizClient
 */
public class CloudAdminBizClient implements AdminBiz {

    public CloudAdminBizClient() {
    }
    public CloudAdminBizClient(String addressUrl, String accessToken) {
        this.addressUrl = addressUrl;
        this.accessToken = accessToken;

        // valid
        if (!this.addressUrl.endsWith("/")) {
            this.addressUrl = this.addressUrl + "/";
        }
    }

    private String addressUrl ;
    private String accessToken;
    private int timeout = 3;


    @Override
    public ReturnT<String> callback(List<HandleCallbackParam> callbackParamList) {
        ProvideClient provideClient = XxlJobSpringCloudExecutor.getApplicationContext().getBean(ProvideClient.class);
        String requestBody = "";
        if (callbackParamList != null) {
            requestBody = GsonTool.toJson(callbackParamList);
        }
        return provideClient.callback(requestBody);
        // return XxlJobRemotingUtil.postBody(addressUrl+"api/callback", accessToken, timeout, callbackParamList, String.class);
    }

    @Override
    public ReturnT<String> registry(RegistryParam registryParam) {
        ProvideClient provideClient = XxlJobSpringCloudExecutor.getApplicationContext().getBean(ProvideClient.class);
        String requestBody = "";
        if (registryParam != null) {
            requestBody = GsonTool.toJson(registryParam);
        }
        return provideClient.registry(requestBody);
        //return XxlJobRemotingUtil.postBody(addressUrl + "api/registry", accessToken, timeout, registryParam, String.class);
    }

    @Override
    public ReturnT<String> registryRemove(RegistryParam registryParam) {
        ProvideClient provideClient = XxlJobSpringCloudExecutor.getApplicationContext().getBean(ProvideClient.class);
        String requestBody = "";
        if (registryParam != null) {
            requestBody = GsonTool.toJson(registryParam);
        }
        return provideClient.registryRemove(requestBody);
        //return XxlJobRemotingUtil.postBody(addressUrl + "api/registryRemove", accessToken, timeout, registryParam, String.class);
    }

}